//
//  CommentModel.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/17.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "CommentModel.h"
#import "CommentRefModel.h"

@implementation CommentModel
{
    CGFloat _cellHeight;
}

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{
             @"ID" : @"id"
             };
}

- (CGFloat)cellHeight{
    //只算一遍
    if (!_cellHeight) {
        
        CGFloat iconW = 30;
        CGFloat nameLabelH = 18;
        CGFloat name_content_margin = 5;
        CGFloat contentFont = 15;
        CGFloat contentRefFont = 14;
        CGFloat timeLabelH = 15;
        
        //计算文字的高度
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - iconW - JKMarginTen * 2 - JKMarginEight, MAXFLOAT);
        //评论内容高度
        CGFloat cmtContentH = [self.content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contentFont]} context:nil].size.height;
        
        //计算cell的高度
        _cellHeight = JKMarginTen + nameLabelH + name_content_margin + cmtContentH + JKMarginTen + timeLabelH + JKMarginTen;
        
        // 没有评论引用，直接返回算好的高度
        if (self.reply_to == nil) return _cellHeight;

        // 评论引用已经被删除
        
        if (self.reply_to.status != 0) {
            _cellHeight += (17 + JKMarginTen);
            return _cellHeight;
        }
        
        // 有评论引用且未被删除
        // 评论引用高度
        NSString *refContent = [NSString stringWithFormat:@"//%@:  %@", self.reply_to.author, self.reply_to.content];
        CGFloat cmtContentRefH = [refContent boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contentRefFont]} context:nil].size.height;
        _cmtRefHeight = cmtContentRefH;
        cmtContentRefH = (cmtContentRefH > 55) ? 34 : cmtContentRefH;
        // 加上引用高度
         _cellHeight += cmtContentRefH + JKMarginTen;
    }
    
    return _cellHeight;
}
@end
